import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_base/provider_demo3/pages/second.dart';
import 'package:flutter_base/provider_demo3/models/task.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => Task(129, '湖北省检察院', TaskStats(30, 0)),
      builder: (_, child) => const MaterialApp(
        home: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home page')),
      body: Center(
        child: Consumer<Task>(
          builder: (context, task, child) => Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('id: ${task.id}', style: const TextStyle(fontSize: 18.0)),
              Text('name: ${task.name}', style: const TextStyle(fontSize: 18.0)),
              Text(
                'total: ${task.stats.total}, completion: ${task.stats.completion}',
                style: const TextStyle(fontSize: 18.0),
              ),
              TextButton(
                  onPressed: () {
                    Navigator.of(context).push(MaterialPageRoute(builder: (_) => SecondPage()));
                  },
                  child: const Text('进入二级页面')),
            ],
          ),
        ),
      ),
    );
  }
}
